package com.ruoyi.project.student.controller;

import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.student.domain.Param.StudentListParam;
import com.ruoyi.project.student.domain.Student;
import com.ruoyi.project.student.domain.vo.StudentListVo;
import com.ruoyi.project.student.service.StudentService;
import com.ruoyi.project.system.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.util.List;


@RestController
@RequestMapping("/system/student")
public class StudentController extends BaseController {
    @Autowired
    private StudentService studentService;

    /**
     * 获取学生列表
     */
    @PreAuthorize("@ss.hasPermi('student:list')")
    @GetMapping("/list")
    //    public TableDataInfo list(Student student)
    public TableDataInfo list(StudentListParam param)
    {
        startPage();
        List<StudentListVo> list = studentService.selectStudentList(param);
//        return list;
        return getDataTable(list);
    }

    /**
     *  添加学生
     */
    @PreAuthorize("@ss.hasPermi('student:add')")
    @PostMapping("/add")
    public AjaxResult addStudent(@Validated @RequestBody Student student){
        int i = 0;
        try {
            i = studentService.insertStudent(student);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return success(i);
    }

    /**
     *  编辑学生
     */
    @PreAuthorize("@ss.hasPermi('student:edit')")
    @PutMapping("/edit")
    public AjaxResult editStudent(@Validated @RequestBody Student student){
      try{
          Student after = studentService.editStudent(student);
          return success(after);
      }catch(Exception e){
          return error(e.getMessage());
        }
    }

    /**
     *  删除学生
     */
    @PreAuthorize("@ss.hasPermi('student:del')")
    @PostMapping("/del")
    public AjaxResult delStudent(@Validated @RequestBody Student student){
        try{
            int num = studentService.delStudent(student);
            return success(num);
        }catch (Exception e){
            return error(e.getMessage());
        }
    }
//    @PreAuthorize("@ss.hasPermi('student:del')")
//    @DeleteMapping("/del/{id}")
//    public AjaxResult delStudent(@PathVariable @NotNull(message = "学生id不能为空") @Min(1 )Integer id) { // 直接接收 id
//        try{
//            return success(studentService.delStudent(id));
//        }catch(Exception e){
//            return error(e.getMessage());
//        }
//
//    }
}
